<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <link type="text/css" rel="stylesheet" th:href="@{/css/all.css}"/>
    <link type="text/css" rel="stylesheet" th:href="@{/plugin/zTree_v3-master/css/metroStyle/metroStyle.css}"/>
    <link type="text/css" rel="stylesheet" th:href="@{/plugin/bootstrap-datetimepicker/bootstrap-datetimepicker.min.css}"/>
<!--
    <script language="JavaScript" th:src="@{/js/all.js}" src="js/all.js"></script>
-->

    <script language='JavaScript' th:src='@{/plugin/jQuery/jQuery-2.2.0.min.js}'></script>
    <script language='JavaScript' th:src="@{/ajax/layer/layer.min.js}"></script>
    <script language='javascript' th:src='@{/js/constant.js}'></script>
    <script language='JavaScript' th:src='@{/plugin/jQuery/jquery.mousewheel.min.js}'></script>
    <script language='JavaScript' th:src='@{/plugin/jquery-easyui-1.5.1/jquery.easyui.min.js}'></script>
    <script language='JavaScript' th:src='@{/plugin/bootstrap/js/bootstrap.min.js}'></script>
    <script language='JavaScript' th:src='@{/plugin/bootstrap-table/bootstrap-table.js}'></script>
    <script language='JavaScript' th:src='@{/plugin/adminLTE/js/app.min.js}'></script>
    <script language='JavaScript' th:src='@{/plugin/lobibox/dist/js/lobibox.js}'></script>
    <script language='JavaScript' th:src='@{/js/network.js}'></script>
    <script language='JavaScript' th:src='@{/js/common.js}'></script>
    <script language='JavaScript' th:src='@{/js/public.js}'></script>
    <script language='JavaScript' th:src='@{/plugin/bootstrap-datetimepicker/js/moment-with-locales.min.js}'></script>
    <script language='JavaScript' th:src='@{/plugin/iCheck/icheck.min.js}'></script>
    <script language='JavaScript' th:src='@{/plugin/zTree_v3-master/js/jquery.ztree.core.min.js}'></script>
    <script language='JavaScript' th:src='@{/plugin/zTree_v3-master/js/jquery.ztree.excheck.min.js}'></script>
    <script language='JavaScript' th:src='@{/plugin/bootstrap-datetimepicker/bootstrap-datetimepicker.min.js}'></script>

    <style type="text/css">
        .form-group{
            margin-top: 5px;
            margin-bottom: 5px;
        }
    </style>
</head>
<body class="body-contrainner fadeInUp">
<div class="box box-info">
    <div class="box-header with-border">
        <form id="form-search" class="form-inline has-info">
            <div class="form-group">
                <label class="control-label">角色名称</label>
                <input type="text" name="role_name@@like" class="form-control" placeholder="请输入角色名称">
            </div>
        </form>
        <div class="box-tools pull-right">
            <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
        </div>
    </div>
    <div class="box-body" style="padding-top: 0px;">
        <div id="tool-bar">
            <button id="btn-search" type="button" class="btn btn-info"><i class="fa fa-search margin-r-5"></i>查询</button>
            <button id="btn-add" type="button" class="btn btn-default"><i class="fa fa-plus margin-r-5"></i>新增</button>
            <button id="btn-edt" type="button" class="btn btn-default"><i class="fa fa-edit margin-r-5"></i>编辑</button>
            <button id="btn-del" type="button" class="btn btn-default"><i class="fa fa-remove margin-r-5"></i>删除</button>
            <button id="btn-export" type="button" class="btn btn-default"><i class="fa fa-download margin-r-5"></i>导出</button>
            <button id="btn-give" type="button" class="btn btn-default"><i class="fa fa-remove margin-r-5"></i>菜单授权</button>

        </div>
        <table id="data-table" class="table table-bordered" data-striped="true" data-pagination="true" data-search="false"
               data-search-on-enter-key="true" data-show-columns="true" data-show-refresh="true"
               data-show-toggle="true" 	data-show-pagination-switch="true" data-id-field="id"
               data-unique-id="id" data-click-to-select="true" data-side-Pagination="server"
               data-cache="false" data-method="get" data-toolbar="#tool-bar" data-query-params="getParam">
            <thead>
            <tr id="tab">

            </tr>
            </thead>
        </table>
    </div>
</div>



<div id="input-modal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title text-info">填写菜单信息</h4>
            </div>
            <div class="modal-body">

                <form id="form-input" class="form-horizontal has-info form-validate">
                    <div class="form-group">
                        <label class="control-label col-md-2 required1">角色名称</label>
                        <div class="col-md-9">
                            <input type="text" class="form-control" name="role_name" placeholder="请输入角色名称" maxlength="40" data-check-nullable="false" >
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label col-md-2 required1">角色描述</label>
                        <div class="col-md-9">
                            <input type="text" class="form-control" name="role_message" placeholder="请输入角色描述" maxlength="40" data-check-nullable="false" >
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button id="btn-cancel" type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button id="btn-save" type="button" class="btn btn-info">保存</button>
            </div>
        </div>
    </div>
</div>


<div id="authorize-dialog"><table id="authorize-table" class="table table-striped"></table></div>
<div id="authorize-dialog-buttons" class="hide">
    <button id="btn-auth-save" type="button" class="btn btn-info">确定</button>
    <button id="btn-auth-cancel" type="button" class="btn btn-cancel">取消</button>
</div>

<div id="input-modal2" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content" style="min-width:300px;max-width: 400px;  width: 70%;" >
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <center><span  style="font-size:20px;">导出字段选择</span></center>
            </div>

            <div class="modal-body" id="messageBody" style="height:340px; overflow:scroll;text-align: center;">
                <div class="btn-group" id="checks" data-toggle="buttons" style="text-align: center;">


                </div>
            </div>
            <div class="modal-footer">
                <center>
                    <button id="btn-selectAll" type="button" class="btn btn-default" >反选</button>
                    <button id="btn-cancel2" type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button id="btn-save2" type="button" class="btn btn-info">保存</button>
                </center>
            </div>
        </div>
    </div>
</div>



<script language="JavaScript">
    var myTableName='sys_role';
    var bootStrap={}

    ajax.postData(publicData.urls.system.getFields, {'tableName':myTableName}, function (data) {
       var html='<th data-field="state" data-checkBox="true">#</th>';
       html+='<th data-field="rowNum" data-align="left" data-halign="left" data-valign="middle" data-formatter="bootstrapFormatter.rowNumFormatter">序号</th>'

       for(var i = 0;i < data.length;++i){
           var codeAndInfo = data[i];
           if(codeAndInfo.column_name == "id"){

           }else{
               html+='<th data-field="'+codeAndInfo.column_name+'" data-align="left" data-halign="left"'
                   +'data-valign="middle" data-sortable="true">'+codeAndInfo.column_comment+'</th>';
           }
       }
       $("#tab").html(html);
       if ( data!=null||data!="") {
           $("#data-table").bootstrapTable("refresh");
           $("#data-table").bootstrapTable({url: publicData.urls.system.getPage});
       }else{
           $("#tab").html("不存在");
       }
   });
   //这个携带页面查询参数
    function getParam(params) {
        var total=commonTools.getBootstrapParams(params, $("#form-search"))
        total.params.tableName=myTableName;
        bootStrap=total;

        return total;
    }

    $("#btn-search").click(function () {
        $("#data-table").bootstrapTable("refresh");
    });

    $("#btn-add").click(function () {
        commonTools.setFormData({}, $("#form-input"));
        $(".icon-show").html("空");
        $("#input-modal").attr("data-save-type", "add");
        $("#input-modal").modal("show");
    });

    $("#btn-edt").click(function () {
        var datas = $("#data-table").bootstrapTable("getSelections");
        if (datas.length == 0) {
            messageBox.info("请选择要修改的数据");
            return;
        }
        if (datas.length > 1) {
            messageBox.info("只能同时修改一条数据");
            return;
        }
        commonTools.setFormData(datas[0], $("#form-input"));
        $(".icon-show").html("<i class='fa fa-fw margin-r-5 "+datas[0].menu_icon+"'></i>"+datas[0].menu_icon);
        $("#input-modal").attr("data-save-type", "edt");
        $("#input-modal").modal("show");
    });



   $("#btn-save").click(function () {
       var datas = $("#data-table").bootstrapTable("getSelections");
        var saveType = $("#input-modal").attr("data-save-type");
        var saveData = $("#form-input").serializeObject();
       var column=new Array();
       var value=new Array();
       var i=0;
       for (var demo in saveData) {
           column[i]=demo;
           value[i]=saveData[demo];
           i++;
       }
       var message={}
       message.column=column;
       message.value=value;
       message.tableName=myTableName;
       if(saveType=='edt'){
           message.id=datas[0].id;
       }

       message.saveType=saveType;
            ajax.postData(publicData.urls.system.addOrEdt,{'message':JSON.stringify(message)}, function (data) {
                $("#input-modal").modal("hide");
                if (saveType == "add") {
                    messageBox.success("添加成功");
                }else {
                    messageBox.success("保存成功");
                }
                $("#data-table").bootstrapTable("refresh");
            });
    });

    $("#btn-del").click(function () {
        var datas = $("#data-table").bootstrapTable("getSelections");
        if (datas.length == 0) {
            messageBox.info("请选择要删除的数据");
            return;
        }

        var ids="";
        for(var i=0;i<datas.length;i++){
            ids+=datas[i].id
            if(i==datas.length-1){

            }else{
                ids+=",";
            }
        }

        messageBox.confirm("您确认要删除该数据吗？", function(){
            ajax.postData(publicData.urls.system.del, {"id": ids,"tableName":"sys_role"}, function () {
                messageBox.success("删除成功");
                $("#data-table").bootstrapTable("refresh");
            });
        }, undefined);
    });
   $("#authorize-table").treegrid({
       url:publicData.urls.system.getTreeGrid,
       idField:'menu_id', treeField:'menu_name', animate: true, width: 585, height:300,
       columns:[
       [
           {title:'菜单名称',field:'menu_name', width: 500},
           {title:'授权',field:'menu_selected', align: "center", width: 70,
               formatter: function(value,row,index){
                   if (row.children == null) {
                       if (value === true) {
                           return "<input type='checkbox' data-menu-id='"+row.menu_id+"' class='square-green' checked='checked'>";
                       } else {
                           return "<input type='checkbox' data-menu-id='"+row.menu_id+"' class='square-green'>";
                       }
                   }
               }
           }
       ]
       ],
       onLoadSuccess: function(data) {
           $("input[type='checkbox'].square-green, input[type='radio'].square-green").iCheck({
               checkboxClass: 'icheckbox_square-green',
               radioClass: 'iradio_square-green'
           });
       }
   });

   $("#authorize-dialog").dialog({
       title: '角色授权', width: 600, height: 400,top:50, modal: true, buttons: "#authorize-dialog-buttons",
       onOpen: function () {
           $("#authorize-dialog-buttons").addClass("show").removeClass("hide");
       }
   });

   $("#btn-give").click(function () {
       var datas = $("#data-table").bootstrapTable("getSelections");
       if (datas.length == 0) {
           messageBox.info("请选择要授权的角色");
           return;
       }
       if (datas.length > 1) {
           messageBox.info("只能同时授权一个角色");
           return;
       }
       var roleId = datas[0].id;
       $('#authorize-table').treegrid('reload',{roleId: roleId});
       $("#authorize-dialog").dialog("open");
   });
   $("#btn-auth-cancel").click(function () {
       $("#authorize-dialog").dialog("close");
   }).click();

   $("#btn-auth-save").click(function () {
       var menuids = "";
       $.each($("input[data-menu-id]"), function (i, n) {
           if ($(n).prop("checked")) {
               menuids += $(n).attr("data-menu-id") + ",";
           }
       });
       var datas = $("#data-table").bootstrapTable("getSelections");
       var params = {
           roleId: datas[0].id,
           menuIds: menuids.substring(0, menuids.length-1)
       };
       console.log(params);
       ajax.postData(publicData.urls.system.setRoleMenu, params, function () {
           $("#authorize-dialog").dialog("close");
           messageBox.success("授权成功");
       });
   });




   //========导出模块

   $("#btn-export").click(function () {
       $("#checks").html("");
       ajax.postData(publicData.urls.system.getFields, {'tableName':myTableName}, function (data) {
           var bb="";
           for(var i = 0;i < data.length;++i){
               var codeAndInfo = data[i];
               bb+="<label class='btn btn-default' style='width: 80%;margin-bottom: 5px;margin-left: 10%;margin-right: 10%;'>"
                   +"<input  name='columnCheckBoxs'    value='"+codeAndInfo.column_name+"' type='checkbox'>"+codeAndInfo.column_comment
                   +"</label>"
           }
           $("#checks").html(bb);
       })
       $("#input-modal2").modal("show");
   })

   $("#btn-save2").click(function () {
       var obj = document.getElementsByName("columnCheckBoxs");
       var check_val =[];
       var columnStr="";
       for(var k in obj){
           if(obj[k].checked)
               columnStr+=obj[k].value+",";
       }
       columnStr=columnStr.substring(0,columnStr.length-1)
       bootStrap.columnStr=columnStr;
       var message=JSON.stringify(bootStrap);
       message=message.replace(/\{/g,"(")
       message= message.replace(/\}/g,")")
       console.log(publicData.urls.system.exportMessage)
       window.location.href=publicData.urls.system.exportMessage+"?message="+message
       $("#input-modal2").modal("hide");
       messageBox.success("删除成功");

   })

   $("#btn-selectAll").click(function () {
       $("#checks .btn").button('toggle')
   })

</script>
</body>
</html>